<?php

/**
 * Material form.
 *
 * @package    sf_sandbox
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class Type0Form extends BaseMaterialForm {
	public function configure() {
		
		unset ( $this ['created_at'], $this ['updated_at'], $this ['user_id'], $this ['click'], $this ['country'], $this ['link'], $this ['custom_int3'], $this ['custom_text1'], $this ['custom_text2'], $this ['custom_text3'], $this ['custom_char2'], $this ['custom_file1'], $this ['custom_file2'], $this ['custom_file3'], $this ['custom_char3'], $this ['seo_title'], $this ['seo_keyword'], $this ['stat'], $this ['city'] );
		
		$c = new Criteria ( );
		$c->add ( FolderPeer::USER_ID, sfContext::getInstance ()->getUser ()->getAttribute ( 'user_id', null, 'sfGuardSecurityUser' ) );
		$c->addJoin ( FolderPeer::ID, Mat2FolderPeer::FOLDER_ID );
		$c->addJoin ( Mat2FolderPeer::MATERIAL_ID, MaterialPeer::ID );
		
		$c->addAnd ( FolderPeer::TREE_PARENT, 1, Criteria::NOT_EQUAL );
		//$c->addAnd(MaterialPeer::ID,$this->getObject()->getId());
		$c->addAnd ( MaterialPeer::ID, 'tt' );
		
		$this->setWidgets ( array ('id' => new sfWidgetFormInputHidden ( ), 
		'title' => new sfWidgetFormInput ( array (), array ('size' => '80' ) ), 
		'custom_char1' => new sfWidgetFormInput ( array (), array ('size' => '45' ) ), 
		'adres' => new sfWidgetFormInput ( array (), array ('size' => '80' ) ), 
		'phone' => new sfWidgetFormInput ( array (), array ('size' => '15' ) ), 
		'email' => new sfWidgetFormInput (  array (),array (), array ('size' => '25' ) ), 
		'photo_path' => new inputImageTag (  array (), array('mat_id'=>$this->object->getId()) ), 
		'short_content' => new sfWidgetFormTextareaNickEdit ( array (), array ('class' => 'material_short_content', 'style' => 'width:650px' ) ), 
		'content' => new sfWidgetFormTextareaNickEdit ( array (), array ('class' => 'material_content', 'style' => 'width:650px' ) ), 
		'see' => new sfWidgetFormInput ( array (), array ('size' => '5' ) ), 
		'custom_int1' => new sfWidgetFormInput ( array (), array ('size' => '5' ) ), 
		'type_id' => new sfWidgetFormInputHidden ( array (), array ('value' => '1', 'class' => 'test' ) ), 
		'custom_int2' => new sfWidgetFormChoice ( array ('choices' => array ('1' => 'активная', '2' => 'неактивная' ) ) ), 
		'pub' => new sfWidgetFormInputCheckbox ( ),
		'mat2_folder_list' => new sfWidgetFormPropelChoiceMany ( array ('model' => 'Folder', 'multiple' => true, 'expanded' => true, 'criteria' => $c ) ) )

		 );
		
		$this->widgetSchema->setLabels ( array ('title' => 'Наименование', 
		'photo_path' => 'Логотип', 
		'custom_file1' => 'Фон', 
		'see' => 'Просмотров', 
		'content' => 'Причание служебное', 
		'seo_title' => 'SEO заголовок', 
		'seo_keyword' => 'SEO слова для поиска', 
		'mat2_folder_list' => 'Выбранные каталоги', 
		'custom_int1' => 'Скидка', 
		'custom_char1' => 'Короткое наименование', 
		'adres' => 'Адрес', 
		'phone' => 'Телефоны', 
		'email' => 'E-mail', 
		'custom_int2' => 'Статус ссылки', 
		'pub' => 'Опубликован', 
		'short_content' => 'Примечание' ) );
		
		$this->validatorSchema ['photo_path'] = new sfValidatorFile ( array ('required' => false, 
		'path' => sfConfig::get ( 'sf_web_dir' ) . '/uploads/material', 
		'mime_types' => 'web_images', 'max_size' => 380000, // 360 kB
        'validated_file_class' => 'sfMatFileValidatedFile' ) );
		
		$this->widgetSchema->setNameFormat ( 'material[%s]' );
		
		$this->validatorSchema->setOption ( 'allow_extra_fields', true );
		$this->widgetSchema->setNameFormat ( 'material[%s]' );
	
	}
	
	public function saveMat2FolderListtt($con = null) {
		
		$mat = $this->getObject ();
		$rel = $mat->getMat2Folders ();
		foreach ( $rel as $fr ) {
			$f = $fr->getFolder ();
			if ($f->hasParent ()) {
				
				$par = $f->retrieveParent ();
				$c = new Criteria ( );
				$c->add ( Mat2FolderPeer::MATERIAL_ID, $mat->getId () );
				$c->add ( Mat2FolderPeer::FOLDER_ID, $par->getId () );
				$cnt = Mat2FolderPeer::doCount ( $c );
				if ($cnt == 0) {
					
					$re = new Mat2Folder ( );
					$re->setMaterialId ( $mat->getId () );
					$re->setFolderId ( $par->getId () );
					$re->save ();
					//$this->getObject()->addMat2Folder($re);
				}
			
			}
		}
		
		parent::saveMat2FolderList ();
		//$c = new Criteria();
	//$c->add(FolderPeer::TREE_PARENT,1);
	//$c->addAnd(FolderPeer::USER_ID,sfContext::getInstance()->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
	//$rf = FolderPeer::doSelectOne($c);
	//$rel = new Mat2Folder();
	//$rel->setFolderId($rf->getId());
	//$rel->setMaterialId($this->object->getPrimaryKey());
	//$rel->save();
	

	}
	
	public function saveMat2FolderList($con = null) {
		if (! $this->isValid ()) {
			throw $this->getErrorSchema ();
		}
		
		if (! isset ( $this->widgetSchema ['mat2_folder_list'] )) {
			// somebody has unset this widget
			return;
		}
		
		if (is_null ( $con )) {
			$con = $this->getConnection ();
		}
		
		$c = new Criteria ( );
		$c->add ( Mat2FolderPeer::MATERIAL_ID, $this->object->getPrimaryKey () );
		Mat2FolderPeer::doDelete ( $c, $con );
		
		$values = $this->getValue ( 'mat2_folder_list' );
		if (is_array ( $values )) {
			
			foreach ( $values as $value ) {
				$obj = new Mat2Folder ( );
				$obj->setMaterialId ( $this->object->getPrimaryKey () );
				$obj->setFolderId ( $value );
				$obj->save ();
				
				$f = FolderPeer::retrieveByPK ( $value );
				
				////Добавляем счетчик
				FolderPeer::recount ( $f );
				
				if ($f->hasParent ()) {
					$par = $f->retrieveParent ();
					
					if (! in_array ( $par->getId (), $values )) {
						
						$this->saverec ( $par, $this->object->getPrimaryKey () );
						////Добавляем счетчик
				        FolderPeer::recount ( $par );
						if ($par->hasParent ()) {
							$parr = $par->retrieveParent ();
							
							if (! in_array ( $parr->getId (), $values )) {
								//print_r($values).'-'.$parr->getId();
						         ////Добавляем счетчик
				                 FolderPeer::recount ( $parr );								

								$this->saverec ( $parr, $this->object->getPrimaryKey () );
								if ($parr->hasParent ()) {
									$parrr = $parr->retrieveParent ();
									if (! in_array ( $parrr->getId (), $values )) {
										$this->saverec ( $parrr, $this->object->getPrimaryKey () );
						                ////Добавляем счетчик
				                        FolderPeer::recount ( $parr );											
									}
								}
							
							}
						
						}
					}
				
				}
			
			}
		}
	}
	
	public function saverec($par, $mat_id) {
		
		//echo $par->getId().'-'.$mat_id;
		$c = new Criteria ( );
		$c->add ( Mat2FolderPeer::FOLDER_ID, $par->getId () );
		$c->addAnd ( Mat2FolderPeer::MATERIAL_ID, $mat_id );
		$cnt = Mat2FolderPeer::doCount ( $c );
		if ($cnt == 0) {
			$re = new Mat2Folder ( );
			$re->setMaterialId ( $mat_id );
			$re->setFolderId ( $par->getId () );
			$re->save ();
		}
	
	}
}
